﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.DataVisualization.Charting;
using System.Drawing;
using System.Data;


public partial class Charts : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session["fileid"] = Request.QueryString["fileid"];
            Session["servicelist"] = Utils.GetAllService(Convert.ToInt32(Session["fileid"]));
            Session["pairflowlist"] = Utils.GetIpPairFlow(Convert.ToInt32(Session["fileid"])); //time comsuming 101.pcap 10s

            //set gvIpPairFlow
            SortDirection = SortDirection.Descending;
            Session["flowSortExpression"] = "totalbytes";
            BindIpPairFlowData("totalbytes");

            //set tooltip for Chart1
            Chart1.Series["Series1"].ToolTip = "#VALX{N0} s\n#VALY{N0} kb/s";
            Chart1.ChartAreas[0].AxisX.Title = "相对时间";
            Chart1.ChartAreas[0].AxisY.Title = "流量(kb/s)";

            //set tooltip for TopFlowPieChart, PieChart
            Series series = TopFlowPieChart.Series["Series1"];
            //series.PostBackValue = "#INDEX";
            //series.LegendPostBackValue = "#INDEX";
            series.LegendText = "#PERCENT";
            TopFlowPieChart.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = true;
            TopFlowPieChart.Series["Series1"]["PieLabelStyle"] = "outside";
            TopFlowPieChart.Series["Series1"]["DoughnutRadius"] = "50";
            TopFlowPieChart.ChartAreas["ChartArea1"].InnerPlotPosition.Width = 80;
            TopFlowPieChart.ChartAreas["ChartArea1"].InnerPlotPosition.Height = 80;
            int topN = 10;
            BindTopFlowData(topN);


            //set tooltip for DelayChart
            DelayChart.ChartAreas["ChartArea1"].AxisX.LabelStyle.Enabled = false;
            DelayChart.Series["Series1"].ToolTip = "#VALX\n#VALY{N3}ms";
            DelayChart.ChartAreas[0].AxisX.LabelStyle.Interval = 1;
            DelayChart.ChartAreas[0].AxisX.LabelStyle.IntervalOffset = 1;
            DelayChart.ChartAreas[0].AxisY.Title = "延时(ms)";
            DelayChart.ChartAreas[0].AxisX.Title = "排行";

            //set gvServiceApp
            List<Utils.ServiceInfo> list = (List<Utils.ServiceInfo>)Session["servicelist"];
            gvServiceApp.DataSource = list;
            gvServiceApp.DataBind();

            //set gvClientNode
            SortDirection = SortDirection.Descending;
            Session["nodeSortExpression"] = "totalclients";
            BindServiceData("totalclients");

            //set ClientNodeChart
            topN = 20;
            if (topN > list.Count)
                topN = list.Count;
            list = list.OrderByDescending(x => x.Totalclients).ToList();
            for (int i = 0; i < topN; i++)
            {
                ClientNodeChart.Series[0].Points.AddXY(list[i].Ip,list[i].Totalclients);
            }
            for (int i = 0; i < topN; i++)
            {
                ClientNodeChart.Series[0].Points[i].AxisLabel = (i + 1).ToString();
                ClientNodeChart.Series[0].Points[i].ToolTip = "服务IP: " + list[i].Ip + "\n端口: " + list[i].Port + "\nClient数目: " + list[i].Totalclients;
            }
            ClientNodeChart.ChartAreas[0].AxisX.LabelStyle.Interval = 1;
            ClientNodeChart.ChartAreas[0].AxisX.LabelStyle.IntervalOffset = 1;
            ClientNodeChart.ChartAreas[0].AxisX.Title = "网络服务";
            ClientNodeChart.ChartAreas[0].AxisY.Title = "节点数";
        }
    }

 
    ////////////////////////////////////////////////////////////////////////////////
    //gvIpPairFlow event
    protected void gvIpPairFlow_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvIpPairFlow.PageIndex = e.NewPageIndex;

        string sortExpression = Session["flowSortExpression"].ToString();
        BindIpPairFlowData(sortExpression);
    }

    protected void gvIpPairFlow_Sorting(object sender, GridViewSortEventArgs e)
    {
        string sortExpression = e.SortExpression;
        Session["flowSortExpression"] = sortExpression;
        if (SortDirection == SortDirection.Ascending)
        {
            SortDirection = SortDirection.Descending;
        }
        else
        {
            SortDirection = SortDirection.Ascending;
        }
        BindIpPairFlowData(sortExpression);
    }

    private void BindIpPairFlowData(string sortExpression)
    {
        List<Utils.IpPairFlow> list = (List<Utils.IpPairFlow>)Session["pairflowlist"];
        if (sortExpression == "totalbytes")
        {
            if (SortDirection == SortDirection.Ascending)
                gvIpPairFlow.DataSource = list.OrderBy(x => x.Totalbytes).ToList();
            else
                gvIpPairFlow.DataSource = list.OrderByDescending(x => x.Totalbytes).ToList();
        }
        else if (sortExpression == "inpackets")
        {
            if (SortDirection == SortDirection.Ascending)
                gvIpPairFlow.DataSource = list.OrderBy(x => x.Inpackets).ToList();
            else
                gvIpPairFlow.DataSource = list.OrderByDescending(x => x.Inpackets).ToList();
        }
        else if (sortExpression == "outpackets")
        {
            if (SortDirection == SortDirection.Ascending)
                gvIpPairFlow.DataSource = list.OrderBy(x => x.Outpackets).ToList();
            else
                gvIpPairFlow.DataSource = list.OrderByDescending(x => x.Outpackets).ToList();
        }
        else if (sortExpression == "inbytes")
        {
            if (SortDirection == SortDirection.Ascending)
                gvIpPairFlow.DataSource = list.OrderBy(x => x.Inbytes).ToList();
            else
                gvIpPairFlow.DataSource = list.OrderByDescending(x => x.Inbytes).ToList();
        }
        else if (sortExpression == "outbytes")
        {
            if (SortDirection == SortDirection.Ascending)
                gvIpPairFlow.DataSource = list.OrderBy(x => x.Outbytes).ToList();
            else
                gvIpPairFlow.DataSource = list.OrderByDescending(x => x.Outbytes).ToList();
        }
        else if (sortExpression == "srcip")
        {
            if (SortDirection == SortDirection.Ascending)
                gvIpPairFlow.DataSource = list.OrderBy(x => x.Srcip).ToList();
            else
                gvIpPairFlow.DataSource = list.OrderByDescending(x => x.Srcip).ToList();
        }
        else if (sortExpression == "dstip")
        {
            if (SortDirection == SortDirection.Ascending)
                gvIpPairFlow.DataSource = list.OrderBy(x => x.Dstip).ToList();
            else
                gvIpPairFlow.DataSource = list.OrderByDescending(x => x.Dstip).ToList();
        }
        gvIpPairFlow.DataBind();
    }
    ////////////////////////////////////////////////////////////////////////////////
    //TopFlowPieChart event
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int topN = Convert.ToInt32(DropDownList1.SelectedItem.Text);
        BindTopFlowData(topN);
    }

    private void BindTopFlowData(int topN)
    {
        Utils.IpPairFlow[] toplist = new Utils.IpPairFlow[topN];

        List<Utils.IpPairFlow> flowlist = (List<Utils.IpPairFlow>)Session["pairflowlist"];
        flowlist = flowlist.OrderByDescending(x => x.Totalbytes).ToList();
        if (topN > flowlist.Count)
            topN = flowlist.Count;
        for (int i = 0; i < topN; i++)
            toplist[i] = flowlist[i];
  
        TopFlowPieChart.Series["Series1"].Points.DataBind(toplist, "srcip", "totalbytes", "");
        //set tooltip
        for (int i = 0; i < topN; i++)
        {
            Utils.IpPairFlow t = toplist[i];
            TopFlowPieChart.Series["Series1"].Points[i].ToolTip = String.Format("{0} -- {1}\n{2:N0} 字节", t.Srcip, t.Dstip, t.Totalbytes);
            TopFlowPieChart.Series["Series1"].Points[i].LegendToolTip = String.Format("{0} -- {1}\n{2:N0} 字节", t.Srcip, t.Dstip, t.Totalbytes);
        }
        TopFlowPieChart.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = true;
        TopFlowPieChart.Series["Series1"]["PieLabelStyle"] = "outside";
        TopFlowPieChart.Series["Series1"]["DoughnutRadius"] = "50";
        TopFlowPieChart.ChartAreas["ChartArea1"].InnerPlotPosition.Width = 90;
        TopFlowPieChart.ChartAreas["ChartArea1"].InnerPlotPosition.Height = 90;
    }
    ////////////////////////////////////////////////////////////////////////////////
    //gvServiceApp event
    protected void gvServiceApp_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvServiceApp.PageIndex = e.NewPageIndex;
        gvServiceApp.DataSource = Utils.GetAllService(Convert.ToInt32(Session["fileid"]));
        gvServiceApp.DataBind();
    }


    ////////////////////////////////////////////////////////////////////////////////
    //gvClientNode event
    protected void gvClientNode_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvClientNode.PageIndex = e.NewPageIndex;
        string sortExpression = Session["nodeSortExpression"].ToString();
        BindServiceData(sortExpression);
    }

    protected void gvClientNode_Sorting(object sender, GridViewSortEventArgs e)
    {
        string sortExpression = e.SortExpression;
        Session["nodeSortExpression"] = sortExpression;
        if (SortDirection == SortDirection.Ascending)
        {
            SortDirection = SortDirection.Descending;
        }
        else
        {
            SortDirection = SortDirection.Ascending;
        }
        BindServiceData(sortExpression);
    }

    //Bind data after sorting for gvClientNode
    private void BindServiceData(string sortExpression)
    {
        List<Utils.ServiceInfo> list = (List<Utils.ServiceInfo>)Session["servicelist"];
        if (sortExpression == "totalclients")
        {
            if(SortDirection == SortDirection.Ascending)
                gvClientNode.DataSource = list.OrderBy(x => x.Totalclients).ToList();
            else
                gvClientNode.DataSource = list.OrderByDescending(x => x.Totalclients).ToList();
        }
        else if (sortExpression == "totalpackets")
        {
            if (SortDirection == SortDirection.Ascending)
                gvClientNode.DataSource = list.OrderBy(x => x.Totalpackets).ToList();
            else
                gvClientNode.DataSource = list.OrderByDescending(x => x.Totalclients).ToList();
        }
        else if (sortExpression == "totalbytes")
        {
            if (SortDirection == SortDirection.Ascending)
                gvClientNode.DataSource = list.OrderBy(x => x.Totalbytes).ToList();
            else
                gvClientNode.DataSource = list.OrderByDescending(x => x.Totalbytes).ToList();
        }
        else if (sortExpression == "port")
        {
            if (SortDirection == SortDirection.Ascending)
                gvClientNode.DataSource = list.OrderBy(x => x.Port).ToList();
            else
                gvClientNode.DataSource = list.OrderByDescending(x => x.Port).ToList();
        }
        else if (sortExpression == "protocol")
        {
            if (SortDirection == SortDirection.Ascending)
                gvClientNode.DataSource = list.OrderBy(x => x.Protocol).ToList();
            else
                gvClientNode.DataSource = list.OrderByDescending(x => x.Protocol).ToList();
        }
        else if (sortExpression == "ip")
        {
            if (SortDirection == SortDirection.Ascending)
                gvClientNode.DataSource = list.OrderBy(x => x.Ip).ToList();
            else
                gvClientNode.DataSource = list.OrderByDescending(x => x.Ip).ToList();
        }

        gvClientNode.DataBind();
    }

    public SortDirection SortDirection
    {
        get
        {
            if (ViewState["SortDirection"] == null)
            {
                ViewState["SortDirection"] = SortDirection.Ascending;
            }
            return (SortDirection)ViewState["SortDirection"];
        }
        set
        {
            ViewState["SortDirection"] = value;
        }
    }
}